package org.jdesktop.databuffer.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;

public class LangUtils {

	public static Map splitStr2Map(String source, String sep1, String sep2)
	  {

	    if ((source != null) && (!source.equals(""))) {
	      Map result = new HashMap();
	      String[] sources = StringUtils.split(source, sep1);
	      for (int i = 0; i < sources.length; i++) {
	        String[] subsources = StringUtils.split(sources[i], sep2);
	        result.put(subsources[0], subsources[1]);
	      }

	      return result;
	    }


	    return null;
	  }

	  public static Map filterNullMap(Map values)
	  {

	    if (values == null) {
	      return null;
	    }

	    Map result = new HashMap();
	    for (Iterator i = values.entrySet().iterator(); i.hasNext(); ) {
	      Map.Entry me = (Map.Entry)i.next();
	      if (me.getValue() != null) {
	        result.put(me.getKey(), me.getValue());
	      }
	    }

	    return result;
	  }

	  public static Map filterEmptyMap(Map values)
	  {

	    if (values == null) {

	      return null;
	    }
	    Map result = new HashMap();
	    for (Iterator i = values.entrySet().iterator(); i.hasNext(); ) {
	      Map.Entry me = (Map.Entry)i.next();
	      String value = (String)me.getValue();
	      if (!StringUtils.isEmpty(value)) {
	        result.put(me.getKey(), me.getValue());
	      }
	    }


	    return result;
	  }

	  public static String getStrFromMapList(List list, String key)
	  {


	    StringBuffer sb = new StringBuffer();
	    for (int i = 0; i < list.size(); i++) {
	      Map map = (Map)list.get(i);
	      if (map.get(key) != null) {
	        sb.append(String.valueOf(map.get(key)) + ",");
	      }
	    }
	    String result = sb.toString();
	    result = StringUtils.substringBeforeLast(result, ",");


	    return result;
	  }

	  public static Map subMap(Map values, String[] keys)
	  {
	    Map result = new HashMap();
	    for (int i = 0; i < keys.length; i++) {
	      Object obj = values.get(keys[i]);
	      if (obj != null) {
	        result.put(keys[i], obj);
	      }
	    }

	    return result;
	  }

	  public static String getMapKeys(Map map)
	  {

	    if (map == null) {
	     

	      return "";
	    }
	    StringBuffer sb = new StringBuffer();
	    for (Iterator i = map.keySet().iterator(); i.hasNext(); ) {
	      sb.append((String)i.next() + ",");
	    }
	    String returnString = StringUtils.substringBeforeLast(sb.toString(), ",");
	   

	    return returnString;
	  }

	  public static String Array2String(Object[] array)
	  {
	    if (array == null) {

	      return "";
	    }
	    StringBuffer sb = new StringBuffer();
	    for (int i = 0; i < array.length; i++) {
	      sb.append(array[i] + ",");
	    }
	    String returnString = StringUtils.substringBeforeLast(sb.toString(), ",");
	   

	    return returnString;
	  }

	  public static Map convertMap(String keyName, String keyValue)
	  {
	   


	    Map pkmap = new HashMap();
	    if (keyName.indexOf("|") > 0) {
	      String[] keynames = StringUtils.split(keyName, "|");
	      String[] keyvalues = StringUtils.split(keyValue, "|");
	      for (int i = 0; i < keynames.length; i++)
	        pkmap.put(keynames[i], keyvalues[i]);
	    }
	    else
	    {
	      pkmap.put(keyName, keyValue);
	    }


	    return pkmap;
	  }

	  public static List getMultiPkList(String keyName, String keyValue)
	  {
	   

	    List keylist = new ArrayList();
	    String[] keyValues = StringUtils.split(keyValue, ",");
	    for (int i = 0; i < keyValues.length; i++) {
	      Map pkmap = new HashMap();
	      String value = keyValues[i];
	      pkmap = convertMap(keyName, value);
	      keylist.add(pkmap);
	    }

	    return keylist;
	  }

	  public static String java2sqlName(String name)
	  {

	    String column = "";
	    for (int i = 0; i < name.length(); i++) {
	      if ((i < name.length() - 1) && (name.charAt(i) >= 'a') && (name.charAt(i) <= 'z') && (name.charAt(i + 1) >= 'A') && (name.charAt(i + 1) <= 'Z'))
	      {
	        column = column + name.charAt(i) + "_";
	      }
	      else column = column + name.charAt(i);
	    }

	    String returnString = column.toLowerCase();
	   

	    return returnString;
	  }

	  public static String sql2javaName(String name)
	  {

	    name = name.toLowerCase();
	    String column = "";
	    for (int i = 0; i < name.length(); i++) {
	      if (name.charAt(i) == '_')
	        column = column + (++i < name.length() ? String.valueOf(name.charAt(i)).toUpperCase() : "");
	      else {
	        column = column + name.charAt(i);
	      }
	    }

	    return column;
	  }
}
